##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(tidyr)
library(tibble)
library(readr)
library(stringr)
library(gt)
library(modelsummary)
source('felm-summary-table.R')


files = paste0('results/',c('future-results-2008-2012.Rdata',
                                        'future-results-2012-2016.Rdata'))

load(files[1])
ModelDemSpExpReps1=ModelDemSpExpReps
ModelDemSpExpOths1=ModelDemSpExpOths
ModelDemSpExpDems1=ModelDemSpExpDems

ModelRepSpExpReps1=ModelRepSpExpReps
ModelRepSpExpOths1=ModelRepSpExpOths
ModelRepSpExpDems1=ModelRepSpExpDems


load(files[2])
ModelDemSpExpReps2=ModelDemSpExpReps
ModelDemSpExpOths2=ModelDemSpExpOths
ModelDemSpExpDems2=ModelDemSpExpDems


ModelRepSpExpReps2=ModelRepSpExpReps
ModelRepSpExpOths2=ModelRepSpExpOths
ModelRepSpExpDems2=ModelRepSpExpDems


varlist1 = c('DemSpExpDiff_nohh' = '$\\Delta$ Dem Exp',
             'hh.d.diff' = '$\\Delta$ HH Democrats',
             'hh.n.diff' = '$\\Delta$ HH Voters',
             'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
             'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
             'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
             'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
             'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
             'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
             'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
             'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
             'MarriageDiff' = '$\\Delta$ Married',
             'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
             'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
             
)

varlist2 = c('RepSpExpDiff_nohh' = '$\\Delta$ Rep Exp',
             'hh.r.diff' = '$\\Delta$ HH Republicans',
             'hh.n.diff' = '$\\Delta$ HH Voters',
             'WhiteBlockGroupDiff' = '$\\Delta$ BG White',
             'AgeBlockGroupDiff' = '$\\Delta$ BG Age',
             'RegsBlockGroupDiff' = '$\\Delta$ BG Reg',
             'HHIncomeBlockGroupDiff' = '$\\Delta$ BG HH Income',
             'HomeownerBlockGroupDiff' = '$\\Delta$ BG Homeowner',
             'YearBuiltBlockGroupDiff' = '$\\Delta$ BG Med. Year Built',
             'DriveWorkBlockGroupDiff' = '$\\Delta$ BG Drive Work',
             'HouseValueBlockGroupDiff' = '$\\Delta$ BG Med. Home Value',
             'MarriageDiff' = '$\\Delta$ Married',
             'CollegeBlockGroupDiff' = '$\\Delta$ BG College',
             'EmplBlockGroupDiff' = '$\\Delta$ BG Unemployed'
             
)


dt = felm.summary.table(summaries = list(ModelDemSpExpReps1, ModelDemSpExpOths1, ModelDemSpExpDems1, ModelDemSpExpReps2, ModelDemSpExpOths2, ModelDemSpExpDems2),
                        output='latex',fmt=3,
                        coef_map=varlist1)

rt = felm.summary.table(summaries = list(ModelRepSpExpReps1, ModelRepSpExpOths1, ModelRepSpExpDems1, ModelRepSpExpReps2, ModelRepSpExpOths2, ModelRepSpExpDems2),
                        output='latex', fmt = 3,
                        coef_map=varlist2)

write_file(dt, 'tables/TabS10a.tex')
write_file(rt, 'tables/TabS10b.tex')
